# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
from datetime import datetime

sys.path.insert(0, os.path.abspath("../.."))


# -- Project information -----------------------------------------------------

project = "darts"
copyright = f"2020 - {datetime.now().year}, Unit8 SA (Apache 2.0 License)"
author = "Unit8 SA"
version = "0.39.0"


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    "sphinx.ext.autosummary",
    "sphinx.ext.autodoc",
    "sphinx.ext.viewcode",
    "sphinx.ext.todo",
    "sphinx.ext.graphviz",
    "sphinx.ext.napoleon",
    "sphinx.ext.githubpages",
    "sphinx_autodoc_typehints",
    "sphinx_copybutton",
    "nbsphinx",
    "m2r2",
    "numpydoc",
    "sphinx_design",
]

exclude_parent_classes = [
    "Module",  # torch.nn.modules.module.Module
    "LightningModule",  # pytorch_lightning.core.module.LightningModule
    "PLForecastingModule",  # darts.models.forecasting.pl_forecasting_module.PLForecastingModule
    "TQDMProgressBar",  #
    "_MultiOutputEstimator",  # sklearn.multioutput._MultiOutputEstimator
    "MultiOutputClassifier",  # sklearn.multioutput.MultiOutputClassifier
    "MultiOutputRegressor",  # sklearn.multioutput.MultiOutputRegressor
    "ndarray",  # numpy.ndarray
]

exclude_members = [
    "min_train_series_length",
    "first_prediction_index",
    "future_covariate_series",
    "past_covariate_series",
    "initialize_encoders",
    "SplitTimeSeriesSequence",
    "randint",
]

autodoc_default_options = {
    "inherited-members": ",".join(exclude_parent_classes),
    "show-inheritance": None,
    "ignore-module-all": True,
    "exclude-members": ",".join(exclude_members),
}

# In order to also have the docstrings of __init__() methods included
autoclass_content = "both"

# Add any paths that contain templates here, relative to this directory.
templates_path = ["templates"]

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = "en"

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
# Note: some source files are excluded from rst generation in the
# darts/docs/Makefile `generate-api` command
exclude_patterns = [
    "_build",
    "Thumbs.db",
    ".DS_Store",
    "**/modules.rst",
]

suppress_warnings = [
    "toc.excluded",  # Suppress warnings about excluded documents from above
]

autosummary_generate = True


# -- numpydoc options --------------------------------------------------------
# (see: https://numpydoc.readthedocs.io/en/latest/install.html)

numpydoc_attributes_as_param_list = False

numpydoc_show_class_members = True

# This might be needed, see https://github.com/numpy/numpydoc/issues/69
numpydoc_class_members_toctree = False


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = "pydata_sphinx_theme"
html_favicon = "static/docs-favicon.ico"

html_theme_options = {
    "github_url": "https://github.com/unit8co/darts",
    "twitter_url": "https://twitter.com/unit8co",
    "logo": {
        "image_light": "static/darts-logo-light.png",
        "image_dark": "static/darts-logo-dark.png",
    },
}


# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["static"]

# Add custom CSS files
html_css_files = ["custom.css"]


# -- Extension configuration -------------------------------------------------

# ignore outputs code blocks when copying (see https://sphinx-copybutton.readthedocs.io/en/latest/use.html#strip-and-configure-input-prompts-for-code-cells)
copybutton_prompt_text = r">>>\s?|\.\.\.\s?"
copybutton_prompt_is_regexp = True

# -- Options for todo extension ----------------------------------------------

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False


# Ensure that otherwise excluded methods get included:
# e.g. include_private_methods = ['_fill_missing_dates', '_restore_xarray_from_frequency']
include_private_methods = []


def skip(app, what, name, obj, skip, options):
    if name in include_private_methods:
        return False
    return skip


# -- Package title and docstring extraction for API documentation ---------------
#
# This function uses the generated *.rst files and processes the generated API documentation to:
# 1. Replace package path titles (e.g., "darts.models.forecasting") with
#    descriptive titles from package docstrings (e.g., "Forecasting Models")
# 2. Insert the full docstring content from package __init__.py files
# 3. Fix inline :doc: link titles to use descriptive names
#
# Note: the *.rst files were generated using the templates in `docs/templates`.
#
# This runs automatically during the Sphinx build via the 'source-read' event.
# Logic is in fix_package_titles.py for easier maintenance.
# ---------------------------------------------------------------------------------

sys.path.insert(0, os.path.abspath(".."))
from fix_package_titles import process_package_docstrings


def setup(app):
    app.connect("autodoc-skip-member", skip)
    app.connect("source-read", process_package_docstrings)
